import { http, type ResultBoolean } from "@/utils/http";

// 数据模型定义
export interface ParkingLot {
  id: number;
  name: string;
  address: string;
  longitude: number;
  latitude: number;
  parkingType: string;
  totalCapacity: number;
  chargeRules: string;
  chargeType: string;
  coverImageUrl: string;
  description: string;
  isEnabled: boolean;
  isDeleted?: boolean;
  sortCode: number;
  createdAt?: string;
  updatedAt?: string;
  config?: string;
  isOnline?: boolean;
  lastHeartbeat?: number; //上次心跳时间
}

export interface ParkingLotVO {
  id: number;
  name: string;
  address: string;
  longitude: number;
  latitude: number;
  parkingType: string;
  totalCapacity: number;
  chargeRules: string;
  chargeType: string;
  coverImageUrl: string;
  description: string;
  isEnabled: boolean;
  sortCode: number;
  createdAt: string;
  updatedAt: string;
}

export interface ParkingLotDTO {
  id?: number;
  name: string;
  address: string;
  longitude: number;
  latitude: number;
  parkingType: string;
  totalCapacity: number;
  chargeRules: string;
  chargeType: string;
  coverImageUrl: string;
  description: string;
  isEnabled: boolean;
  sortCode: number;
}

export interface ParkingLotQueryDTO {
  queryId?: string;
  pageNum?: number;
  pageSize?: number;
  queryStartTime?: string;
  queryEndTime?: string;
  orderByColumn?: string;
  isAsc?: string;
  name?: string;
  address?: string;
  parkingType?: string;
  isEnabled?: boolean;
}

export interface ParkingDetailResult {
  parkingLot: ParkingLot;
  images: Array<{
    id: number;
    relatedType: string;
    parkingLotId: number;
    poiId: number;
    imageUrl: string;
    description: string;
    createdAt: string;
  }>;
  isFavorite: boolean;
}

export interface PageResultParkingLot {
  pageNum: number;
  pageSize: number;
  totalPage: number;
  total: number;
  list: ParkingLot[];
}

export interface IPageParkingLotVO {
  records: ParkingLotVO[];
  total: number;
  size: number;
  current: number;
  orders: Array<{
    column: string;
    asc: boolean;
  }>;
  optimizeCountSql: boolean;
  searchCount: boolean;
  optimizeJoinOfCountSql: boolean;
  maxLimit: number;
  countId: string;
  pages: number;
}

// 原有接口保留并优化
export const getNearestParking = (params: {
  latitude: number;
  longitude: number;
  limit: number;
}) => {
  return http.request<ParkingLot>("get", "/api/parking/nearest", {
    params
  });
};

export const getNearbyParking = (params: {
  latitude: number;
  longitude: number;
  radius: number;
  pageNum: number;
  pageSize: number;
}) => {
  return http.request<PageResultParkingLot>("get", "/api/parking/nearby", {
    params
  });
};

export const getParkingDetail = (id: number) => {
  return http.request<ParkingDetailResult>("get", `/api/parking/${id}`);
};

export const favoriteParking = (id: number) => {
  return http.request<ResultBoolean>("post", `/api/parking/favorite/${id}`);
};

export const cancelFavoriteParking = (id: number) => {
  return http.request<ResultBoolean>("delete", `/api/parking/favorite/${id}`);
};

// 新增停车场管理接口
/** 分页条件查询ParkingLot */
export const getParkingPage = (data?: ParkingLotQueryDTO) => {
  return http.request<ResultBoolean>("post", "/api/parking/page", { data });
};

/** 新增ParkingLot */
export const addParking = (data?: ParkingLotDTO) => {
  return http.request<ResultBoolean>("post", "/api/parking/add", { data });
};

/** 编辑ParkingLot */
export const editParking = (data?: ParkingLotDTO) => {
  return http.request<ResultBoolean>("post", "/api/parking/edit", { data });
};

/** 删除ParkingLot */
export const deleteParking = (id: number) => {
  return http.request<ResultBoolean>("post", `/api/parking/delete/${id}`);
};

/** 手动触发配置下发 */
export const sendConfig = (params: { clientId: number }) => {
  return http.request<ResultBoolean>("get", `/api/parking/sendConfigToClient`, {
    params
  });
};

/** 查询所有ParkingLot（可带条件） */
export const getParkingList = (data?: ParkingLotQueryDTO) => {
  return http.request<ParkingLotVO>("post", "/api/parking/list", {
    data
  });
};
